<?php

namespace App\Admin\Controllers\Sample;

use App\Admin\Controllers\BaseAdminController;
use App\Admin\Forms\OrderSampleChecker;
use App\Admin\Forms\OrderSampleReport;
use App\Admin\Forms\Pdl1Edit;
use App\Models\Order\OrderSample;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Modal;

class OrderSampleController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(OrderSample::with(['goodsOrder', 'checkItem', 'checker', 'binder', 'qrcode', 'parameters', 'checkPdf']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('goodsOrder.order_no', '订单编号');
            $grid->column('sample_sn', '检测码');
            $grid->column('checkItem.name', '项目名称');
            $grid->column('qrcode.url', '二维码')->image('', 50, 50);
//            $grid->column('is_used');
            $grid->column('status')->using(trans('order-sample.options.status'));
            $grid->column('binder.name', '绑定用户');
            $grid->column('checker_id', '检测者')->display('编辑')->modal(function ($model) {
                $model->title('编辑');
                return OrderSampleChecker::make()->payload(['id' => $this->id]);
            });
            $grid->column('name');
            $grid->column('gender')->using([1 => '男', 2 => '女']);
            $grid->column('age');
            $grid->column('relationship_type')->using(trans('family-user.options.identity'));
            $grid->column('phone');
            $grid->column('id_card');
            $grid->column('report_result', '检测结果')->if(function () {
                return $this->status == 3;
            })->display('编辑')->modal(function ($modal) {
                $modal->title('编辑报告');
                return OrderSampleReport::make()->payload(['id' => $this->id]);
            })->if(function () {
                return $this->status == 4;
            })->display('查看')->modal(function ($modal) {
                $modal->title('查看报告');
                return OrderSampleReport::make()->payload(['id' => $this->id]);
            });
            $grid->column('checkPdf', '检测报告')->display(function ($pdf) {
                return @$pdf['url'] ? "<a href='/api/v1/download?id={$pdf['id']}&filename={$this->sample_sn}.pdf' target='_blank'>Pdf报告</a>" : '';
            })->width('120px');
//            $grid->column('check_pdf_id');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->like('name')->width('300px');
                $filter->like('phone')->width('300px');
                $filter->equal('status')->select(trans('order-sample.options.status'))->width('300px');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OrderSample(), function (Show $show) {
            $show->field('id');
            $show->field('order_id');
            $show->field('item_id');
            $show->field('qrcode_id');
            $show->field('is_used');
            $show->field('status');
            $show->field('user_id');
            $show->field('name');
            $show->field('gender');
            $show->field('age');
            $show->field('relationship_type');
            $show->field('contact');
            $show->field('id_card');
            $show->field('check_result');
            $show->field('check_pdf_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new OrderSample(), function (Form $form) {
            $form->display('id');
            $form->text('order_id');
            $form->text('item_id');
            $form->text('qrcode_id');
            $form->text('is_used');
            $form->text('status');
            $form->text('user_id');
            $form->text('name');
            $form->text('gender');
            $form->text('age');
            $form->text('relationship_type');
            $form->text('contact');
            $form->text('id_card');
            $form->text('check_result');
            $form->text('check_pdf_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
